package hos.file.utils;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.util.Log;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * <p>Title: AssetUtils </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2024-07-05 13:14
 */
public class AssetUtils {
    private static final int BUFFER_SIZE = 8192;

    private AssetUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }


    /**
     * Copy the file from assets.
     *
     * @param assetsFilePath The path of file in assets.
     * @param destFilePath   The path of destination file.
     * @return {@code true}: success<br>{@code false}: fail
     */
    public static boolean copyFileFromAssets(Context context, final String assetsFilePath, final String destFilePath) {
        boolean res = true;
        try {
            String[] assets = context.getAssets().list(assetsFilePath);
            Log.d("TAG", "copyFileFromAssets: "+Arrays.toString(assets));
            String[] list = context.getAssets().list("");
            Log.d("TAG", "copyFileFromAssets: "+Arrays.toString(list));
            if (assets != null && assets.length > 0) {
                for (String asset : assets) {
                    res &= copyFileFromAssets(context, assetsFilePath + "/" + asset, destFilePath + "/" + asset);
                }
            } else {
                res = FileIOUtils.writeFileFromIS(
                        destFilePath,
                        context.getAssets().open(assetsFilePath)
                );
            }
        } catch (IOException e) {
            e.printStackTrace();
            res = false;
        }
        return res;
    }

    /**
     * Return the content of assets.
     *
     * @param assetsFilePath The path of file in assets.
     * @return the content of assets
     */
    public static String readAssets2String(Context context, final String assetsFilePath) {
        return readAssets2String(context, assetsFilePath, null);
    }

    /**
     * Return the content of assets.
     *
     * @param assetsFilePath The path of file in assets.
     * @param charsetName    The name of charset.
     * @return the content of assets
     */
    public static String readAssets2String(Context context, final String assetsFilePath, final String charsetName) {
        try {
            InputStream is = context.getAssets().open(assetsFilePath);
            byte[] bytes = ConvertUtils.inputStream2Bytes(is);
            if (bytes == null) return "";
            if (FileUtils.isSpace(charsetName)) {
                return new String(bytes);
            } else {
                try {
                    return new String(bytes, charsetName);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                    return "";
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * Return the content of file in assets.
     *
     * @param assetsPath The path of file in assets.
     * @return the content of file in assets
     */
    public static List<String> readAssets2List(Context context, final String assetsPath) {
        return readAssets2List(context, assetsPath, "");
    }

    /**
     * Return the content of file in assets.
     *
     * @param assetsPath  The path of file in assets.
     * @param charsetName The name of charset.
     * @return the content of file in assets
     */
    public static List<String> readAssets2List(Context context, final String assetsPath,
                                               final String charsetName) {
        try {
            return ConvertUtils.inputStream2Lines(context.getResources().getAssets().open(assetsPath), charsetName);
        } catch (IOException e) {
            e.printStackTrace();
            return Collections.emptyList();
        }
    }


}
